<template>
  <div>
    <!-- 面包屑导航区 -->
    <bread-nav>
      <span slot="c1">数据统计</span>
      <span slot="c2">数据报表</span>
    </bread-nav>

    <!-- 卡片视图区 -->
    <el-card>
      <div id="main" style="width: 800px; height: 400px"></div>
    </el-card>
  </div>
</template>

<script>
import BreadNav from 'components/content/nav/BreadNav.vue'
import * as echarts from 'echarts'
import { getChartsData } from 'network/reports.js'
import _ from 'lodash'
export default {
  name: 'Report',
  components: { BreadNav },
  data() {
    return {
      // 需要合并的对象
      options: {
        title: {
          text: '用户来源',
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross',
            label: {
              backgroundColor: '#E9EEF3',
            },
          },
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true,
        },
        xAxis: [
          {
            boundaryGap: false,
          },
        ],
        yAxis: [
          {
            type: 'value',
          },
        ],
      },
    }
  },
  created() {},
  // DOM 元素渲染完执行钩子
  async mounted() {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('main'))
    // 获取数据并绘制图表
    const { data: res } = await getChartsData()
    console.log(res.data.xAxis[0].data)
    if (res.meta.status !== 200) {
      this.$message.error('获取报表数据失败！')
      return
    }
    // 准备数据和配对项
    const resulet = _.merge(res.data, this.options) // _.merge lodash提供的合并对象的方法
    // 展示图表
    myChart.setOption(resulet)
  },
}
</script>

<style scoped>
</style>